// 打开数据库
function openDB(db_name){
	plus.sqlite.openDatabase({
		name: 'share',
		path: '_doc/'+db_name,
		success: function(e){
			console.log('openDatabase success!');
		},
		fail: function(e){
			console.log('openDatabase failed: '+JSON.stringify(e));
		}
	});
}

// 关闭数据库
function closeDB(){
	plus.sqlite.closeDatabase({
		name: 'share',
		success: function(e){
			console.log('closeDatabase success!');
		},
		fail: function(e){
			console.log('closeDatabase failed: '+JSON.stringify(e));
		}
	});
}

// 判断数据库是否打开
function isOpenDb(db_name){
	return plus.sqlite.isOpenDatabase({
		name: 'share',
		path: '_doc/'+db_name
	});
}

// 执行事务
function transactionDB(){
	plus.sqlite.transaction({
		name: 'share',
		operation: 'begin',
		success: function(e){
			console.log('transaction success!');
		},
		fail: function(e){
			console.log('transaction failed: '+JSON.stringify(e));
		}
	});
}

function init_table(){
	var sql = 'create table if not exists orderlist("book_id" CHAR(50),book_tag" CHAR(50),"book_auth" CHAR(100))';
	executeSQL(sql,function(event){
		console.log(JSON.stringify(event));
	})
}

function executeSQL(sql,callback){
	plus.sqlite.executeSql({
		name: 'share',
		sql: sql,
		success: callback,
		fail: function(e){
			console.log('executeSql failed: '+JSON.stringify(e));
		}
	});
}

function selectSQL(sql,callback){
	plus.sqlite.selectSql({
		name: 'share',
		sql: 'select * from database',
		success: callback,
		fail: function(e){
			console.log('selectSql failed: '+JSON.stringify(e));
		}
	});
}

module.exports = {
	init_table:init_table,
	openDB:openDB,
	isOpenDb:isOpenDb
}
